package org.practice.aspectj;


import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
/**
 * 基于Aspect的切面，要另外引入AspectJ类库(aspectjrt.jar,aspectjweaver.jar)
 * AspectJ是面向切面编程的框架,专门用来实现动态代理(AOP编程)的类库
 * 除了使用aop:aspect标签，还有用@Aspect注解的方式代替配置文件，配置文件只需配置<aop:aspectj-autoproxy/>
 * @author jack
 */
//@Aspect
public class TimeHandler {
	private Logger logger = Logger.getLogger(this.getClass().getName());
	
	//@Around(pointcut="execution(* org.practice.service..*.*(..))")
	public Object around(ProceedingJoinPoint point) throws Throwable {
		long procTime = System.currentTimeMillis();
        logger.log(Level.INFO, point.getArgs()[0] + " 开始执行 " + point.getSignature().getName() + "方法"); 
        try {
        	Object result = point.proceed();
        	return result;
        }finally {
        	//计算执行时间
        	procTime = System.currentTimeMillis() - procTime;
            logger.log(Level.INFO, point.getArgs()[0] + " 执行 " + point.getSignature().getName() + "方法结束");
            logger.log(Level.INFO, "执行 " + point.getSignature().getName() + " 方法共用了 " + procTime + "毫秒");
        }
	}

}

